package com.my.storage.store;

import org.springframework.stereotype.Component;

import java.io.*;

/**
 * @author xhw
 */
@Component
public class FileUtils {

    /**
     * 保存文件到目标路径。
     *
     * @param inputStream 文件输入流
     * @param targetFile  目标文件
     * @throws IOException 如果保存文件时出现错误
     */
    public static void saveFile(InputStream inputStream, File targetFile) throws IOException {
        try (OutputStream outputStream = new FileOutputStream(targetFile)) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }
    }

}